<?php

namespace app\live\controller;

use app\common\controller\Adminbase;
use app\model\Feed as FeedModel;

class Video extends Adminbase
{
    public function index()
    {
        $model = 'Feed';

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $list   = FeedModel::where($map)
                ->field('*')
                ->withAttr('product_price', function ($value) {
                    return $value > 0 ? number_format($value / 100, 2) : '0.00';
                })
                ->withAttr('commission', function ($value) {
                    return $value > 0 ? number_format($value / 100, 2) : '0.00';
                })
                ->withAttr('status', function ($value) {
                      $text = [1 => "<span class='layui-badge layui-bg-green'>显示</span>", 0 => "<span class='layui-badge'>隐藏</span>"];
                    return $text[$value];
                })
                ->page($page, $pageSize)
                 ->order('status desc, sort asc, id desc')
                ->select();
            $total  = FeedModel::where($map)->count();
            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setAddBtn(false)
            ->setDelAllBtn(false)
            ->setDataUrl()
            ->setFormUrl('form')
            ->setLimit()
            ->setModel($model)
            ->addTableCheckBox()
            ->setSwitchTpl('switchTpl', 'index_status')
            ->setSwitchTpl('imageTpl', 'product_img_url')
            ->addTableColumn('id', 'ID', null, null, true)
            ->addTableColumn('sort', '排序', null, null, false, '', false, '', true)
            ->addTableColumn('talent_appid', '合作达人ID', 200)
            ->addTableColumn('product_img_url', '商品图片', null, '#imageTpl')
            ->addTableColumn('product_name', '名称', 400)
            ->addTableColumn('product_price', '售价（元）')
            ->addTableColumn('commission', '总佣金（元）')
            ->addTableColumn('index_status', '首页是否显示', null, '#switchTpl', false, '', false)
            ->addTableColumn('status', '状态')
            ->addTableOperation()
            ->returnForm();

        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

    public function form()
    {
        $model = 'Feed';

        $id = $this->request->param('id/d');
        if (isset($id) && $id > 0) {
            $info = FeedModel::get($id);
            $this->assign('id', $id);
            $this->assign('action', 2);
        } else {
            $info = [
                'sort' => 50,
                'type' => 2,
            ];
        }

        $builder = new \app\common\controller\FormBuilder();
        $builder
            ->setModel($model)
            ->setSuccessUrl()
            ->addFormItem('product_name', 'text', '名称')
            ->addFormItem('sort', 'gtnumber', '排序', '值越小排序越靠前')
            ->addFormItem('index_status', 'radio', '首页是否显示', '', [1 => '显示', 0 => '隐藏'])
            ->addFormItem('status', 'radio', '状态', '', [1 => '显示', 0 => '隐藏'])
            ->setFormData($info)
            ->returnForm();
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }

}
